package com.woniuxy.kaer.system.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.kaer.commons.jwtUtil.JwtUtil;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.redis.util.RedisKey;
import com.woniuxy.kaer.system.annotation.RequirePermission;
import com.woniuxy.kaer.system.annotation.RequireRole;
import com.woniuxy.kaer.system.dto.LoginManagerDTO;
import com.woniuxy.kaer.system.dto.ManagerDTO;
import com.woniuxy.kaer.system.form.LoginManagerForm;
import com.woniuxy.kaer.system.form.ManagerForm;
import com.woniuxy.kaer.system.model.Manager;
import com.woniuxy.kaer.system.param.LoginManagerParam;
import com.woniuxy.kaer.system.param.ManagerParam;
import com.woniuxy.kaer.system.service.ManagerService;
import com.woniuxy.kaer.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sun
 * @since 2025年02月17日
 */
@RestController
@Api(tags = "管理员管理")
@RequestMapping("/manager")
public class ManagerController {
    @Resource
    private ManagerService managerService;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RoleService roleService;

    //新增管理员
    @ApiOperation(value = "新增管理员")
    @PostMapping("/addManager")
    @RequireRole("BOSS")
    @RequirePermission("新增管理员")
    public Result addManager(@RequestBody ManagerForm form)throws Exception{
        ManagerParam param = BeanUtil.toBean(form, ManagerParam.class);
        managerService.addManager(param);
        return Result.succeed("新增管理员成功！",null);
    }
    //管理员登录
    @ApiOperation(value = "管理员登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginManagerForm form, HttpServletResponse response)throws Exception{
        LoginManagerParam param = BeanUtil.toBean(form, LoginManagerParam.class);
        LoginManagerDTO LoginManagerDTO=managerService.login(param);
        System.out.println("LoginManagerDTO: " + LoginManagerDTO);
        //生成令牌
        Map<String, Object> body = BeanUtil.beanToMap(LoginManagerDTO);
        String accessToken = jwtUtil.createToken(body,15);
        String refreshToken = jwtUtil.createToken(body, 3*24*60);
        //保存短token
        stringRedisTemplate.opsForValue().set(RedisKey.REFRESH_ACCESS_EXPIRE(accessToken),refreshToken,3 * 24 * 60, TimeUnit.MINUTES);

        //保存长token
        stringRedisTemplate.opsForValue().set(RedisKey.ACCESS_EXPIRE(accessToken),"",30, TimeUnit.MINUTES);
        System.out.println("Access Token: " + accessToken);
        System.out.println("Refresh Token: " + refreshToken);
        response.setHeader("token", accessToken);
        response.setHeader("Access-Control-Expose-Headers","token");


        return Result.succeed("登录成功！",LoginManagerDTO);
    }
    //查询所有管理员
    @RequireRole("BOSS")
    @RequirePermission("查询所有管理员")
    @ApiOperation(value = "查询所有管理员")
    @GetMapping("/findAllManager")
    public Result findAllManager()throws Exception{
        List<ManagerDTO> managers =managerService.findAllManager();
        if (managers.isEmpty()) {
            throw new Exception("管理员列表为空！");
        }
        return Result.succeed("查询成功！",managers);
    }
    //获取当前登录管理员的managerId
    @ApiOperation(value = "获取当前登录管理员的managerId")
    @GetMapping("/getLoggedInAdmin")
    public Result getLoggedInAdmin(@RequestHeader("token") String token)throws Exception{
        //通过token获取ID
        Integer managerId = jwtUtil.getManagerId1(token);
        if (managerId == null) {
            throw new Exception("获取管理员ID失败！");
        }
        return Result.succeed("获取管理员ID成功！",managerId);
    }




}

